package com.example.administrator.smartbutler.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.administrator.smartbutler.MainActivity;
import com.example.administrator.smartbutler.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名:SmartButler
 * 包名:com.example.administrator.smartbutler.ui
 * 文件名：GuideAvtivity
 * 创建者：justin
 * Created by Administrator on 2018/1/15.
 * 描述：TODO
 */

public class GuideActivity extends AppCompatActivity implements View.OnClickListener {


    private ViewPager mViewPager;
    //容器
    private List<View> viewList = new ArrayList<>();

    private View view1, view2, view3;

    private ImageView dot1, dot2, dot3;

    private ImageView iv_back;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_guide);

        initView();
    }

    private void initView() {

        //设置小圆点图片
        dot1 = (ImageView) findViewById(R.id.dot1);
        dot2 = (ImageView) findViewById(R.id.dot2);
        dot3 = (ImageView) findViewById(R.id.dot3);

        //设置默认图片
        setDot(true, false, false);


        //设置页面
        mViewPager = (ViewPager) findViewById(R.id.mViewPager);

        view1 = View.inflate(this, R.layout.pager_item_one, null);
        view2 = View.inflate(this, R.layout.pager_item_two, null);
        view3 = View.inflate(this, R.layout.pager_item_three, null);

        view3.findViewById(R.id.btn_gotoHomepage).setOnClickListener(this);

        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);

        iv_back = (ImageView) findViewById(R.id.iv_back);

        iv_back.setOnClickListener(this);


        //设置适配器
        mViewPager.setAdapter(new GuideAdapter());

        //设置监听器
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            //切换pager
            @Override
            public void onPageSelected(int position) {

                switch (position) {
                    case 0:
                        setDot(true, false, false);
                        iv_back.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        setDot(false, true, false);
                        iv_back.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        setDot(false, false, true);
                        iv_back.setVisibility(View.GONE);
                        break;

                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_gotoHomepage:
            case R.id.iv_back:
                startActivity(new Intent(this, LoginActivity.class));
                finish();
                break;

        }

    }

    class GuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return viewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            ((ViewPager) container).addView(viewList.get(position));

            return viewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            container.removeView(viewList.get(position));

        }
    }

    private void setDot(boolean isCheck1, boolean isCheck2, boolean isCheck3) {

        if (isCheck1) {
            dot1.setBackgroundResource(R.drawable.big);
        } else {
            dot1.setBackgroundResource(R.drawable.dot_medium);
        }

        if (isCheck2) {
            dot2.setBackgroundResource(R.drawable.big);
        } else {
            dot2.setBackgroundResource(R.drawable.dot_medium);
        }


        if (isCheck3) {
            dot3.setBackgroundResource(R.drawable.big);
        } else {
            dot3.setBackgroundResource(R.drawable.dot_medium);
        }

    }

}
